﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using com.yd.sdk.api;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;

namespace com.yd.sdk
{
    public class ManifestVO
    {
        public Dictionary<string,string> dependencies;
        public List<ScopedRegistry> scopedRegistries;
    }

    public class ScopedRegistry
    {
        public string name;
        public string url;
        public List<string> scopes;
    }

    public static class SDKMenuItem
    {
        [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法

        // [MenuItem("YD_SDK_MANAGER/Admob/Setup Admob")]
        // public static void SetupAdmob()
        // {
        //     var path = Application.dataPath.Replace("Assets", "/Packages/manifest.json");
        //     var sr = new StreamReader(File.Open(path, FileMode.Open));
        //     var vo = JsonConvert.DeserializeObject<ManifestVO>(sr.ReadToEnd());
        //
        // }
        
        
        [MenuItem("YD_SDK_MANAGER/CREATE CONFIG")]
        public static void CreateSettings()
        {
            if (!Directory.Exists(Application.dataPath + "/Resources"))
                Directory.CreateDirectory(Application.dataPath + "/Resources");
            
            
            if (File.Exists(Application.dataPath +"/Resources/SDK_CONFIG.asset"))
            {
                var sdkSettings = AssetDatabase.LoadAssetAtPath<SdkSettings>("Assets/Resources/SDK_CONFIG.asset");
                Selection.activeObject = sdkSettings;
            }
            else
            {
                var sdkSettings = ScriptableObject.CreateInstance<SdkSettings>();
                AssetDatabase.CreateAsset(sdkSettings,"Assets/Resources/SDK_CONFIG.asset");
                AssetDatabase.SaveAssets();
                Selection.activeObject = sdkSettings;
            }




            // File.Exists(Application.dataPath)
            //
            // AssetDatabase.CreateFolder("Assets", "Resources1");
            //
            // AssetDatabase.CreateAsset(sdkSettings,"Assets/Resources/SDK_Config.asset");
            // AssetDatabase.SaveAssets();
            // AssetDatabase.Refresh();
        }

        // [MenuItem("YD_SDK_MANAGER/Admob/Admob Document")]
        // public static void AdmobDocument()
        // {
        //     
        // }
        //
        //
        // [MenuItem("YD_SDK_MANAGER/Fyber/Setup Fyber")]
        // public static void SetupFyber()
        // {
        //     //add
        //     var registry = new ScopedRegistry()
        //     {
        //         name = "npmjs",
        //         url = "https://registry.npmjs.org/",
        //         scopes = new List<string>(){"com.fyber.fairbid.unity"}
        //     };
        //     AddScopedRegistry(registry);
        //     AddDependencies("com.fyber.fairbid.unity","3.17.0");
        //
        //
        // }
        //
        // [MenuItem("YD_SDK_MANAGER/Fyber/Fyber Document")]
        // public static void FyberDocument()
        // {
        //     Application.OpenURL("https://developer.fyber.com/hc/en-us/articles/360010151197-Initialize-the-SDK");
        // }
        
        
        // [MenuItem("YD_SDK_MANAGER/ResetPackage")]
        // public static void ResetPackage()
        // {
        //     var returnInt = MessageBox(IntPtr.Zero, "是否重置所有package", "reset package", 1);
        //     if (returnInt == 1)
        //     {
        //         var source = Application.dataPath.Replace("Assets", Constants.ORIGIN_PACKAGE_MANIFEST);
        //         var dst = Application.dataPath.Replace("Assets", Constants.PACKAGE_MANIFEST);
        //         if(File.Exists(dst))File.Delete(dst);
        //         File.Copy(source,dst);
        //         AssetDatabase.Refresh();
        //     }
        // }

        
        // [MenuItem("YD_SDK_MANAGER/SerManifest")]
        // public static void SerManifest()
        // {
        //     //var source = Application.dataPath.Replace("Assets", "/Packages/yd_sdkmanager/Plugins/Android/Admob/AndroidManifest.xml");
        //     var source = Application.dataPath.Replace("Assets", Constants.GOOGLE_BILLING_MANIFEST);
        //     var dest = Application.dataPath + Constants.ANDROID_MAIN_MANIFEST;
        //     Debug.Log(source +"," + dest);
        //     Utils.MergeAndroidManifest(source,dest);
        // }


        
        /// <summary>
        /// 添加依赖关系包
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="version"></param>
        private static void AddDependencies(string packageName,string version)
        {
            var path = Application.dataPath.Replace("Assets", "/Packages/manifest.json");
            var value = File.ReadAllText(path);
            var vo = JsonConvert.DeserializeObject<ManifestVO>(value);
            
            if (vo.dependencies.ContainsKey(packageName))
                Debug.Log($"已经添加了{packageName}的包");
            else
                vo.dependencies.Add(packageName,version);

            File.WriteAllText(path,JsonConvert.SerializeObject(vo));
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 添加注册范围
        /// </summary>
        /// <param name="registry"></param>
        private static void AddScopedRegistry(ScopedRegistry registry)
        {
            var path = Application.dataPath.Replace("Assets", "/Packages/manifest.json");
            var value = File.ReadAllText(path);

            var vo = JsonConvert.DeserializeObject<ManifestVO>(value);

            if (vo.scopedRegistries!=null && vo.scopedRegistries.FindIndex(p => p.name.Equals(registry.name)) != -1)
                Debug.Log($"已经添加了{registry.name}的registry");
            else
            {
                if (vo.scopedRegistries == null)
                    vo.scopedRegistries = new List<ScopedRegistry>();
                vo.scopedRegistries.Add(registry);
            }

            File.WriteAllText(path,JsonConvert.SerializeObject(vo));
            AssetDatabase.Refresh();
        }
        
        
    }
    
}
